package kerjodando.casper.tools;

import java.awt.*;
import java.awt.event.*;
import ants.p2p.gui.FrameAnt;
import kerjodando.casper.util.ConstantUtil;

public class Tray {
	private static SystemTray tray;
	private static TrayIcon trayIcon;
	private static FrameAnt mainFrame;
	private static Object lock = new Object();
	public static boolean isSupported=false;
	private static volatile boolean bIsShown = false;

	public static void hiddenFrame() {
		if (tray != null && trayIcon != null) {
			synchronized (lock) {
				if (!bIsShown) {
					try {
						tray.add(trayIcon);
						if (Tray.mainFrame.isVisible()) {
							Tray.mainFrame.setVisible(false);
						}
					} catch (AWTException e) {
						// System.err.println("TrayIcon could not be added.");
					}
					bIsShown = true;
				}
			}
		}
	}

	public static void showFrame() {
		if (tray != null && trayIcon != null) {
			synchronized (lock) {
				if (bIsShown) {
					tray.remove(trayIcon);
					if (!Tray.mainFrame.isVisible()) {
						Tray.mainFrame.setVisible(true);
						Tray.mainFrame.setState(Frame.NORMAL);
					}
					bIsShown = false;
				}
			}
		}
	}

	public static void setupTray(FrameAnt mainFrame) {
		Tray.isSupported=SystemTray.isSupported();
		if (trayIcon == null && Tray.isSupported) {
			Tray.mainFrame = mainFrame;

			tray = SystemTray.getSystemTray();
			Image image = Toolkit.getDefaultToolkit().getImage(
					Tray.class.getClassLoader().getResource(
							"anims/kerjo_tray.gif"));

			/*
			 * MouseListener mouseListener = new MouseListener() {
			 * 
			 * public void mouseClicked(MouseEvent e) {
			 * 
			 * if (e.getButton() == MouseEvent.BUTTON1) {
			 * FrameAnt.getInstance("").setVisible(true);
			 * FrameAnt.getInstance("").setState(Frame.NORMAL); } }
			 * 
			 * public void mouseEntered(MouseEvent e) { }
			 * 
			 * public void mouseExited(MouseEvent e) { }
			 * 
			 * public void mousePressed(MouseEvent e) { }
			 * 
			 * public void mouseReleased(MouseEvent e) { } };
			 */

			ActionListener exitListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// System.out.println("Exiting...");
					Tray.mainFrame.onClose(true);
				}
			};
			ActionListener actionListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					showFrame();
				}
				/*
				 * public void actionPerformed(ActionEvent e) {
				 * trayIcon.displayMessage("Action Event", "An Action Event Has
				 * Been Performed!", TrayIcon.MessageType.INFO); }
				 */
			};

			PopupMenu popup = new PopupMenu();
			MenuItem defaultItem = new MenuItem("Exit");
			defaultItem.addActionListener(exitListener);
			popup.add(defaultItem);

			MenuItem restoreItem = new MenuItem("Restore");
			restoreItem.addActionListener(actionListener);
			popup.add(restoreItem);

			trayIcon = new TrayIcon(image, ConstantUtil.APPLICATION, popup);

			trayIcon.setImageAutoSize(true);
			trayIcon.addActionListener(actionListener);
			// trayIcon.addMouseListener(mouseListener);
		}
	}
}
